let oUser = document.querySelector('.user')
let oPass = document.querySelector('.password')
// 再次验证密码
let passwordsame = document.querySelector('.passwordsame')
// 获取短信验证码
let sendyan = document.querySelector('.sendyan')
let yan1 = document.querySelector('.yan1')
let birth = document.querySelector('.birth')
// 注册按钮
let oReg = document.querySelector('.t-in')
// 勾选阅读安全协议
let abtn = document.querySelector(".abtn")
// 提示字符
let anumber = document.querySelector(".anumber")
// 节流
var flag = true
function fn() {
    // console.log(oUser.value, oPass.value);
    if (flag) {
        pAjax({
            url: '../server/register.php',
            type: 'post',
            data: {
                username: oUser.value,
                password: oPass.value
            }
        }).then(function (res) {
            // 此时是字符串，要变成对象
            // console.log(res);
            let json = JSON.parse(res)
            // console.log(res);
            // console.log(json);    //{code: 0, msg: "注册成功", token: "12345343243vbvdbfnf"}

            if (json.err === 1) {
                alert(json.msg)
                // 成功
                // 存一个localStorage
                localStorage.setItem('token', '8923jsdsdk9sdhosj92ol2')
                // 跳转到index
                   window.location.href = './login.html'
            } else {
                alert(json.msg)
                // 打开节流
                flag = true
            }
        })
    }
    flag = false
}
oPass.addEventListener('keydown', function (ev) {
    if (ev.keyCode === 13) {
        // 是否勾选协议
        if (abtn.checked) {
            anumber.innerHTML = " ";
            fn()
        } else {
            anumber.innerHTML = "请勾选并同意协议 !";
        }
    }
})
oReg.addEventListener('click', function () {
    // 是否勾选协议
    if (abtn.checked) {
        anumber.innerHTML = " ";
        fn()
    } else {
        anumber.innerHTML = "请勾选并同意协议 !";
    }

})

// 正则判断开始
oUser.onblur = function () {
    var nmNum = oUser.value;
    var re = /^\w{3,20}$/g;
    var res = re.test(nmNum);
    if (res == true) {
        anumber.innerHTML = " ";
    } else if (nmNum == '') {
        anumber.innerHTML = "用户名不为空 ! ";

    } else {
        anumber.innerHTML = "用户名格式错误 ! "
    }
}
oPass.onblur = function () {
    var oPassNum = oPass.value;
    // 大小写字母和数字
    var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{8,20}$/g;
    var res = re.test(oPassNum);
    if (res == true) {
        anumber.innerHTML = " ";
    } else if (oPassNum == '') {
        anumber.innerHTML = "密码不为空 !";
    } else {
        anumber.innerHTML = "请输入8~20字符，需同时包含大小写英文和数字 !";
    }
}
passwordsame.onblur = function () {
    var oPassNum = oPass.value;
    var sameNum = passwordsame.value;
    if (oPassNum !== sameNum) {
        anumber.innerHTML = "密码两次输入不一致 !";
    } else {
        anumber.innerHTML = " ";
    }
}



